package com.seaboxdata.scheduler;

import com.seaboxdata.dao.HdfsDao;
import com.seaboxdata.dao.HiveDataDao;
import com.seaboxdata.entity.TableConfig;
import com.seaboxdata.service.TableConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @create 2021-02-24 11:03
 **/
@Component
@Configuration
public class DeletePartitionJobTest {

    @Autowired
    private TableConfigService tableConfigService;

    @Autowired
    private HdfsDao hdfsDao;

    @Autowired
    private HiveDataDao hiveDataDao;

    /*@Scheduled(cron = "${partition.delete}")
    public void doCollect() throws Exception {
        List<TableConfig> list = tableConfigService.list();
        for (TableConfig tableConfig : list) {
            List<String> history_partitions = new ArrayList<>();
            List<String> landing_partitions = new ArrayList<>();
            List<String> delete_partitions = new ArrayList<>();
            String tableName = tableConfig.getTableName();
            Connection connection = hiveDataDao.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("show partitions history_db." + tableName);
            while (resultSet.next()) {
                history_partitions.add(resultSet.getString("partition"));
            }

            ResultSet resultSetLanding = statement.executeQuery("show partitions landing_db." + tableName.replaceFirst("h", "l"));
            while (resultSetLanding.next()) {
                landing_partitions.add(resultSetLanding.getString("partition"));
            }

            for (String history_partition : history_partitions) {
                if (!landing_partitions.contains(history_partition)) {
                    delete_partitions.add(history_partition);
                }
            }

            for (String delete_partition : delete_partitions) {
                statement.execute("alter table history_db." + tableConfig.getTableName() + " drop partition(data_dt_iso = '" + delete_partition.substring(12, 22) + "')");
            }
        }
         }
*/

}
